package com.csdj.t309.aop;




import com.csdj.t309.common.Role;
import org.aopalliance.intercept.Joinpoint;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
public class AopLogger {

    private Logger log = Logger.getLogger("aoplogger");

    @Pointcut("execution(public * com.csdj.t309.service.*.add*(..))")
    public void pointcut(){};

    @Pointcut("@annotation(com.csdj.t309.common.Role)")
    public void annotation(){};

    @Before("execution(public * com.csdj.t309.service.*.add*(..))")
    public void before(JoinPoint jp){ //joinpoint 连接点
        Object[] args = jp.getArgs();//切入点参数
        Object target = jp.getTarget();//增强目标
        Signature signature = jp.getSignature();
        signature.getDeclaringType();  //切入点的方法点
        System.out.print("参数：");
        for(Object obj :args){
            log.info(obj.toString());
        }
        log.info("切入点的方法名：");
        log.info(signature.getName());
//        log.info("增强目标：");
//        System.out.println(target.toString());
    }

    @AfterThrowing(value = "pointcut()",throwing = "e")
    public void afterThrowning(Exception e){

    }

    @Before(value = "annotation()")
    public void print(JoinPoint jp){
        MethodSignature signature = (MethodSignature)jp.getSignature();
        Method method = signature.getMethod();
        Role annotation = method.getAnnotation(Role.class);
        String value = annotation.value();
        System.out.println(value);
    }
}
